package org.srtc.infopub.datacenter.storage;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.Set;

import org.srtc.infopub.tools.TestXML;


public class TestQuitTable {

	public HashMap<String, ArrayList<String>> quitTable;
	
	private TestXML tXML;
	
	
	public TestQuitTable() {
		quitTable = new HashMap<String, ArrayList<String>>();	
	}
	
	
	public void appendQuitTable(HashMap<String, ArrayList<String>> table) {
		Set<Entry<String, ArrayList<String>>> set = table.entrySet();
		Iterator<Entry<String, ArrayList<String>>> it = set.iterator();
		
		while (it.hasNext()) {
			Entry<String, ArrayList<String>> en = it.next();
			this.quitTable.put(en.getKey(), en.getValue());
		}
	}
	
	
	public void createQuitTable(String dirPath) {
		File file = new File(dirPath);
		if (file.isDirectory()) {
			String[] files = file.list();
			for (int i = 0; i<files.length; i++) {
				if (!files[i].endsWith(".xml")) {
					continue;
				}
				tXML = new TestXML(dirPath + "/" + files[i]);
				tXML.getQuiteTable();
				appendQuitTable(tXML.quiteTable);				
			}		
		}
	}
	
	
	/**
	 * 得到矿所有叶子
	 * @param dirPath 信息类配置文件所在目录
	 * @return 没有叶节点则返回null
	 */
	public HashMap<String, ArrayList<String>> coalLeaves(TestCoalTable tct, String dirPath) {
		
		HashMap<String, ArrayList<String>> map = new HashMap<String, ArrayList<String>>();
		
		if (tct.getCoals().isEmpty()) {
			tct.coalInfos();
		}
		if (quitTable.isEmpty()) {
			createQuitTable(dirPath);
		}
		
		Iterator<Entry<String, ArrayList<String>>> it = tct.getCoals().entrySet().iterator();
		while (it.hasNext()) {
			ArrayList<String> list = new ArrayList<String>();
			
			Entry<String, ArrayList<String>> en = it.next();
			ArrayList<String> infos = en.getValue();
		
			for (int i = 0; i<infos.size(); i++) {
				ArrayList<String> leaves = quitTable.get(infos.get(i) + ".");
//				System.out.println(leaves);
				list.addAll(leaves);
			}
			map.put(en.getKey(), list);
		}
		
		return map;
	}
	
	
	public void getQuitTable() {
		TestCoalTable tct = new TestCoalTable("data_source_define/coalInfo.xml");
		coalLeaves(tct, "info_define");
		appendQuitTable(coalLeaves(tct, "info_define"));
	}

	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		TestQuitTable tqt = new TestQuitTable();
		tqt.getQuitTable();
		System.out.println(tqt.quitTable);
	}

}
